package com.example.springboot.config.exceptionadvice;

import com.example.springboot.entity.ResultData;
import com.example.springboot.exception.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/11/26 15:12
 */
@ControllerAdvice
@RestController // 返回json形式数据，不然会跳转Thymeleaf页面

@Slf4j
public class ExceptionAdvice {

    public ResultData<String> exception(FeignException feignException) {
        log.error("业务异常 ex={}", feignException.getMessage(), feignException);
        return ResultData.fail(feignException.getMessage());
    }

    @ExceptionHandler
    public ResultData<String> catchIdempotentException(RuntimeException runtimeException) {
        log.error("业务异常 ex={}", runtimeException.getMessage(), runtimeException);
        return ResultData.fail(runtimeException.getMessage());
    }
}
